"use strict";
var chatRemote = require('../remote/chatRemote');

module.exports = function(app) {
	return new Handler(app);
};

var Handler = function(app) {
	this.app = app;
};

var handler = Handler.prototype;

/**
 * Send messages to users
 *
 * @param {Object} msg message from client
 * @param {Object} session
 * @param  {Function} next next stemp callback
 *
 */
handler.send = function(msg, session, next) {
	var rid = session.get('rid');
	var username = session.uid.split('*')[0];
	var channelService = this.app.get('channelService');
	var param = {
		msg: msg.content,
		from: username,
		target: msg.target
	};
	channel = channelService.getChannel(rid, false);

	//the target is all users
	if (msg.target == '*') {
		channel.pushMessage('onChat', param);
	}
	//the target is specific user
	else {
		var tuid = msg.target + '*' + rid;
		var tsid = channel.getMember(tuid)['sid'];
		channelService.pushMessageByUids('onChat', param, [{
			uid: tuid,
			sid: tsid
		}]);
	}
	next(null, {
		route: msg.route
	});
};


const FM = require("./FoodManager");
const FoodEffect = FM.FoodEffect;
const Food = FM.Food;
const FoodSet = FM.FoodSet;
const FoodSetManager = FM.FoodSetManager;

const food_set_manager = FM.food_set_manager;

const range_width = FM.range_width;
const range_height = FM.range_height;

const unit_range_width = FM.unit_range_width;
const unit_range_height = FM.unit_range_height;

const x_len = range_width / unit_range_width;
const x_len_2 = x_len / 2;
const y_len = range_height / unit_range_height;
const y_len_2 = y_len / 2;

handler.getRangeFoods = function(msg, session, next) {
	const uid = session.uid;
	const foodRemote = this.app.rpc.chat.foodRemote;
	foodRemote.getRangeFoods(session, msg.x, msg.y, msg.width, msg.height, function(res) {
		next(null, {
			foods: res
		});
	});
};

handler.eatFoods = function(msg, session, next) {
	const rid = session.get('rid');
	const uid = session.uid;
	const username = session.uid.split('*')[0];
	const channelService = this.app.get('channelService');
	const channel = channelService.getChannel(rid, false);
	const snakeRemote = this.app.rpc.chat.snakeRemote;
	const foodRemote = this.app.rpc.chat.foodRemote;
	snakeRemote.get(session, uid, function(my_snake_info) {
		if (!my_snake_info) {
			throw new Error("Snake No Init.");
		}

		var my_snake = my_snake_info;
		my_snake_info.__proto__ = Snake.prototype;
		foodRemote.removeFoods(session, msg.foods, function(res) {
			res.forEach((food) => {
				food.__proto__ = Food.prototype;
				food.effectFood(my_snake);
			});
			snakeRemote.set(session, uid, my_snake, function(my_snake_info) {
				next(null, my_snake_info);
			});
			channel.pushMessage('foodBeEated', {
				foods: res,
				snake: my_snake
			});
		});

	});
};
/** 蛇
 *
 */
const SM = require("./SnakeMamager");

const Snake = SM.Snake;
const SnakeSet = SM.SnakeSet;
const SnakeSetManager = SM.SnakeSetManager;
const snakeMap = new Map();

function getSessionSnake(session) {
	return snakeMap.get(session.uid);
};

function setSessionSnake(session, snake) {
	return snakeMap.set(session.uid, snake || null);
};

handler.getMySnake = function(msg, session, next) {
	const rid = session.get('rid');
	const username = session.uid.split('*')[0];
	const channelService = this.app.get('channelService');
	const channel = channelService.getChannel(rid, false);
	const uid = session.uid;
	const snakeRemote = this.app.rpc.chat.snakeRemote;
	// console.log("snakeRemote:", snakeRemote);
	var snake_info = snakeRemote.get(session, uid, function(snake_info) {
		// console.log("snakeRemote.get", snake_info)

		if (!snake_info) {
			snakeRemote.set(session, uid, {
				id: uid,
				rid: rid,
				body_length: 60,
				density: 20,
				speed: 15,
				sensitivity: 240,
				direction: 90,
				delay_num: 0,
			}, (snake_info) => {
				// console.log("snakeRemote.set", snake_info)
				next(null, snake_info);
				channel.pushMessage('joinSnake', snake_info);
			});
		} else {
			next(null, snake_info);
		}
	});
}
handler.getOtherSnakes = function(msg, session, next) {
	const uid = session.uid;
	const snakeRemote = this.app.rpc.chat.snakeRemote;
	snakeRemote.getOther(session, uid, (snake_info_list) => {
		// console.log("snakeRemote.getOther", snake_info_list)
		next(null, {
			snakes: snake_info_list
		})
	});
}
handler.turnMySnakeHead = function(msg, session, next) {
	const uid = session.uid;
	const snakeRemote = this.app.rpc.chat.snakeRemote;
	snakeRemote.turnHead(session, uid, msg.deg, (snake_info) => {
		// console.log("snakeRemote.turnHead", snake_info)
		// console.log("uid", uid);
		next(null, snake_info);
	});
}

handler.dieSnake = function(msg, session, next) {
	const uid = session.uid;
	const snakeRemote = this.app.rpc.chat.snakeRemote;
	snakeRemote.die(session, uid, function(data) {
		next(null, {
			success: !!data
		})
	});
}

//冲刺
handler.snakeSprint = function(msg, session, next) {
	const uid = session.uid;
	const snakeRemote = this.app.rpc.chat.snakeRemote;
	snakeRemote.sprint(session, uid, function(data) {
		next(null, {
			success: !!data
		});
	});
}

//停止冲刺
handler.stopSnakeSprint = function(msg, session, next) {
	const uid = session.uid;
	const snakeRemote = this.app.rpc.chat.snakeRemote;
	snakeRemote.stopSprint(session, uid, function(data) {
		next(null, {
			success: !!data
		});
	});
}